package com.kly.user.traffic;

import lombok.extern.slf4j.Slf4j;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 获取到的流量
 */
@Slf4j
public class GavenTraffic {

    /**
     * 今日建联总次数
     */
    private Integer todayMatchCnt;

    private String userCode;

    //系统给与的wlm次数
    private Integer getWlmCount;

    private Date lastWlmtime;

    //系统给的likeback次数
    private Integer getLikeBackCount;

    private Date lastLikeBackTime;


    //系统给的强制建联次数
    private Integer getForceConnectCount;

    private Date lastForceConnectTime;

    public Integer getTodayMatchCnt() {
        return todayMatchCnt;
    }

    public GavenTraffic setTodayMatchCnt(Integer todayMatchCnt) {
        this.todayMatchCnt = todayMatchCnt;
        return this;
    }

    public boolean wlmRateCheckPass(Integer gap, Integer maxCnt) {

        Instant endTime = Instant.now(); // 当前时间

        // 将 java.util.Date 转换为 Instant
        Instant startInstant = lastWlmtime.toInstant();

        // 计算分钟差
        long minutesDifference = Duration.between(startInstant, endTime).toMinutes();


        return minutesDifference > gap && getWlmCount < maxCnt;
    }

    public boolean likebackRateCheckPass(Integer restLikeCnt, Integer gap, Integer maxCnt) {
        if (restLikeCnt <= 0) {
            return false;
        }

        Instant endTime = Instant.now(); // 当前时间

        // 将 java.util.Date 转换为 Instant
        Instant startInstant = lastLikeBackTime.toInstant();

        // 计算分钟差
        long minutesDifference = Duration.between(startInstant, endTime).toMinutes();


        return minutesDifference > gap && getLikeBackCount < maxCnt * 0.6;
    }

    public boolean forceConnectRateCheckPass(Integer gap, Integer maxCnt) {

        Instant endTime = Instant.now(); // 当前时间

        // 将 java.util.Date 转换为 Instant
        Instant startInstant = lastForceConnectTime.toInstant();

        // 计算分钟差
        long minutesDifference = Duration.between(startInstant, endTime).toMinutes();


        return minutesDifference > gap && getForceConnectCount < maxCnt;
    }

    public boolean allowSystemToGiveTraffic(Integer gap) {
        Instant endTime = Instant.now(); // 当前时间
        Instant startInstant = lastForceConnectTime.toInstant();

        log.info("==TrafficAssignTask 检测上次下发时间  ==，gap:{},lastForceConnectTime:{},lastLikeBackTime:{},lastWlmtime:{}", gap, lastForceConnectTime, lastLikeBackTime, lastWlmtime);

        // 计算分钟差
        long minutesDifferenceFc = Duration.between(startInstant, endTime).toMinutes();
        log.info("==TrafficAssignTask 检测上次下发时间  ==，minutesDifferenceFc:{}", minutesDifferenceFc);
        // 计算分钟差
        long minutesDifferenceLB = Duration.between(lastLikeBackTime.toInstant(), endTime).toMinutes();
        log.info("==TrafficAssignTask 检测上次下发时间  ==，minutesDifferenceLB:{}", minutesDifferenceLB);
        long minutesDifferenceWlm = Duration.between(lastWlmtime.toInstant(), endTime).toMinutes();
        log.info("==TrafficAssignTask 检测上次下发时间  ==，minutesDifferenceWlm:{}", minutesDifferenceWlm);

        long minValue = minutesDifferenceFc;

        if (minutesDifferenceLB < minValue) {
            minValue = minutesDifferenceLB;
        }

        if (minutesDifferenceWlm < minValue) {
            minValue = minutesDifferenceWlm;
        }
        log.info("==TrafficAssignTask 检测上次下发时间  ==，userCode:{},minValue:{},gap:{},res:{}",userCode, minValue,gap,minValue > gap);
        return minValue > gap;
    }

    public static void main(String[] args) {

    }

    public String getUserCode() {
        return userCode;
    }

    public GavenTraffic setUserCode(String userCode) {
        this.userCode = userCode;
        return this;
    }

    public Integer getGetWlmCount() {
        return getWlmCount;
    }

    public GavenTraffic setGetWlmCount(Integer getWlmCount) {
        this.getWlmCount = getWlmCount;
        return this;
    }

    public Integer getGetLikeBackCount() {
        return getLikeBackCount;
    }

    public GavenTraffic setGetLikeBackCount(Integer getLikeBackCount) {
        this.getLikeBackCount = getLikeBackCount;
        return this;
    }

    public Integer getGetForceConnectCount() {
        return getForceConnectCount;
    }

    public GavenTraffic setGetForceConnectCount(Integer getForceConnectCount) {
        this.getForceConnectCount = getForceConnectCount;
        return this;
    }

    public Date getLastWlmtime() {
        return lastWlmtime;
    }

    public GavenTraffic setLastWlmtime(Date lastWlmtime) {
        this.lastWlmtime = lastWlmtime;
        return this;
    }

    public Date getLastLikeBackTime() {
        return lastLikeBackTime;
    }

    public GavenTraffic setLastLikeBackTime(Date lastLikeBackTime) {
        this.lastLikeBackTime = lastLikeBackTime;
        return this;
    }

    public Date getLastForceConnectTime() {
        return lastForceConnectTime;
    }

    public GavenTraffic setLastForceConnectTime(Date lastForceConnectTime) {
        this.lastForceConnectTime = lastForceConnectTime;
        return this;
    }
}
